package cn.tedu.tea.web;

import lombok.Data;

import java.io.Serializable;

@Data
public class R<T> implements Serializable {

    private int code;

    private String msg;

    private T data;

    public static <T> R<T> ok(){
        return restResult(null,ServiceCode.OK.getCode(),ServiceCode.OK.getMsg());
    }

    public static <T> R<T> ok(T data){
        return restResult(data,ServiceCode.OK.getCode(),ServiceCode.OK.getMsg());
    }

    public static <T> R<T> ok(T data,ServiceCode serviceCode){
        return restResult(data,serviceCode.getCode(),serviceCode.getMsg());
    }

    public static <T> R<T> fail(){
        return restResult(null,ServiceCode.ERR_CONFLICT.getCode(),ServiceCode.ERR_CONFLICT.getMsg());
    }

    public static <T> R<T> fail(T data){
        return restResult(data,ServiceCode.ERR_CONFLICT.getCode(),ServiceCode.ERR_CONFLICT.getMsg());
    }

    public static <T> R<T> fail(T data,ServiceCode serviceCode){
        return restResult(data,serviceCode.getCode(),serviceCode.getMsg());
    }

    public static <T> R<T> fail(T data,Integer code,String msg){
        return restResult(data,code,msg);
    }


    private static <T> R<T> restResult(T data,Integer code,String msg){
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
